﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using iTextSharp.text;
using System.IO;
namespace WZ.UtilityLibray.PDF
{
    public class ReaderContent
    {
        static ReaderContent()
        {
            RegisterFont();
        }
        static bool isRegister = false;
        static void RegisterFont()
        {
            if (!isRegister)
            {
                FontFactory.Register(Environment.GetFolderPath(Environment.SpecialFolder.System) +
                                       @"\..\Fonts\STSONG.ttf");
                FontFactory.Register(Environment.GetFolderPath(Environment.SpecialFolder.System) +
                                     @"\..\Fonts\simhei.ttf");
                FontFactory.Register(Environment.GetFolderPath(Environment.SpecialFolder.System) +
                                     @"\..\Fonts\simsun.ttc");
            }
            isRegister = true;
        }
        public static string Read(string pdfUri)
        {
            try
            {
                PdfReader pdfReader = new PdfReader(pdfUri);
                int numberOfPages = pdfReader.NumberOfPages;
                string text = string.Empty;

                for (int i = 1; i <= numberOfPages; ++i)
                {
                    byte[] bufferOfPageContent = pdfReader.GetPageContent(i);
                    text += System.Text.Encoding.UTF8.GetString(bufferOfPageContent);
                }
                pdfReader.Close();

                return text;
            }
            catch (Exception ex)
            {
                return null;
            }  
        }
        [Obsolete]
        public static string ReadPDFContent(string pdfUri)
        {
            StringBuilder sb = new StringBuilder(); ;
            PdfReader reader = new PdfReader(pdfUri);
            //StreamWriter output = new StreamWriter(new FileStream(dest, FileMode.Create));
            int pageCount = reader.NumberOfPages;
            for (int pg = 1; pg <= pageCount; pg++)
            {
                // we can inspect the syntax of the imported page
                byte[] streamBytes = reader.GetPageContent(pg);
                PRTokeniser tokenizer = new PRTokeniser(new RandomAccessFileOrArray(streamBytes));
                while (tokenizer.NextToken())
                {
                    switch (tokenizer.TokenType)
                    {
                        case PRTokeniser.TokType.COMMENT:
                            break;
                        case PRTokeniser.TokType.ENDOFFILE:
                            break;
                        case PRTokeniser.TokType.END_ARRAY:
                            break;
                        case PRTokeniser.TokType.END_DIC:
                            break;
                        case PRTokeniser.TokType.NAME:
                            break;
                        case PRTokeniser.TokType.NUMBER:
                            break;
                        case PRTokeniser.TokType.OTHER:
                            break;
                        case PRTokeniser.TokType.REF:
                            break;
                        case PRTokeniser.TokType.START_ARRAY:
                            break;
                        case PRTokeniser.TokType.START_DIC:
                            break;
                        case PRTokeniser.TokType.STRING:
                            sb.Append(tokenizer.StringValue);
                            break;
                        default:
                            break;
                    }
                   
                }
            }
            return sb.ToString();
        }
    }
}
